// 方案一
var express = require('express');
var proxy = require('http-proxy-middleware');
const path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, './')));//静态资源index.html和node代码在一个目录下
app.use('/', proxy({
    // 代理跨域目标接口
    target: 'http://192.168.1.53:80',
    changeOrigin: true,

    // 修改响应头信息，实现跨域并允许带cookie
    onProxyRes: function(proxyRes, req, res) {
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Credentials', 'true');
    },

    // 修改响应信息中的cookie域名
    cookieDomainRewrite: false  // 可以为false，表示不修改
}));

app.listen(3000);
console.log('Proxy server is listen at port 3000...');

// 方案二
"use strict";
const express = require('express');
const path = require('path');
const app = express();
const request = require('request');

// 配置静态文件服务中间件
var serverUrl='http://octest.wechat.c.citic/';//server地址
app.use(express.static(path.join(__dirname, './')));//静态资源index.html和node代码在一个目录下
app.use('/', function(req, res) {
    var  url = serverUrl + req.url;
    req.pipe(request(url)).pipe(res);
});
app.listen(3000,'127.0.0.1', function () {//前端ajax地址写 http://127.0.0.1:3000/
    console.log('server is running at port 3000');
});